package mapper;

import com.my.javabean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserMapper {
    List<User> selectAll();
    List<User> selectByUsernameAndPassword(@Param("username") String username,@Param("password") String password);

    @Insert("insert into user(username,password,phone)values(#{username},#{password},#{phone})")
    int insertUser(User user);
    @Delete("delete from user where uid=#{uid}")
    int deleteByUid(int uid);
    @Update("update user set password=#{password} where uid=#{uid}")
    int updatePasswordByUid(@Param("uid") int uid,@Param("password") String password);
    @Select("select * from user where uid=#{uid}")
    User selectByUid(int uid);

    @Select("select uid,username uname,password pwd,phone tel from user")
    @Results(id = "u",value = {
            @Result(id = true,column = "uid" ,property = "uid"),
            @Result(column = "uname" ,property = "username"),
            @Result(column = "pwd" ,property = "password"),
            @Result(column = "tel" ,property = "phone")
    })
    List<User> selectUser();

    @Select("select uid,username uname,password pwd,phone tel from user")
    @ResultMap("u")
    List<User> selectUser1();


    @Select("select * from user")
    @Results(id = "u_r" ,value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(column = "uid",property = "r",one = @One(
                    select = "mapper.RoleMapper.selectByUid",
                    fetchType = FetchType.LAZY
            ))
    })
    List<User> selectUser_Role();

    @Select({"<script>        select * from user\n" +
            "        <where>\n" +
            "            <if test=\"uid!=0\">and uid=#{uid}</if>\n" +
            "            <if test=\"username!=null\">and username=#{username}</if>\n" +
            "            <if test=\"password!=null\">and password=#{password}</if>\n" +
            "            <if test=\"phone!=null\">and phone=#{phone}</if>\n" +
            "        </where></script>"})
    List<User> selectByUser(User user);

}